//
//  XMBrowsHistoryCell.m
//  XMJJ_zml
//
//  Created by 丁浩 on 2025/5/28.
//

#import "XMBrowsHistoryCell.h"

@interface XMBrowsHistoryCell ()

@property (nonatomic, strong) UIImageView *contentImgView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UILabel *priceLabel;
@property (nonatomic, strong) UILabel *dateLabel;
@property (nonatomic, strong) UIImageView *avatarImgView;
@property (nonatomic, strong) UILabel *userNameLabel;
@property (nonatomic, strong) UILabel *creditLabel;

@end

@implementation XMBrowsHistoryCell

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self creatUI];
    }
    return self;
}

- (void)creatUI {
    self.backgroundColor = kColorHEX(0xF7F7F7);
    UIView *contentView = [[UIView alloc] initWithFrame:self.bounds];
    contentView.backgroundColor = kColorHEX(0xffffff);
    [contentView setCornerRadius:KScaleWidth(13.0)];
    [self addSubview:contentView];
    
    [contentView addSubview:self.contentImgView];
    [contentView addSubview:self.titleLabel];
    [contentView addSubview:self.priceLabel];
    [contentView addSubview:self.dateLabel];
    [contentView addSubview:self.avatarImgView];
    [contentView addSubview:self.userNameLabel];
    [contentView addSubview:self.creditLabel];
}

- (void)setContentWithGoodDic:(XMMyBrowseProductModel *)productModel {
//    _contentImgView.image = [UIImage imageWithColor:kColorHEX(0x00ffff)];
    [_contentImgView sd_setImageWithURL:[NSURL URLWithString:productModel.serviceCover]];
    _titleLabel.text = productModel.serviceTitle;//@"探索未知，感受...";
    NSString *priceStr = [NSString stringWithFormat:@"¥%@", productModel.price];
    NSMutableAttributedString  *priceAttr = [[NSMutableAttributedString alloc] initWithString:priceStr attributes:@{NSFontAttributeName : KBoldFontSize(18.0), NSForegroundColorAttributeName : kColorHEX(0xDD4B39)}];
    [priceAttr setAttributes:@{NSFontAttributeName : KFontSize(12.0), NSForegroundColorAttributeName : kColorHEX(0xDD4B39)} range:NSMakeRange(0, 1)];
    [_priceLabel setAttributedText:priceAttr];
    NSInteger currentInter = [[NSDate date] timeIntervalSince1970];
    NSInteger distanceInter = currentInter - [productModel.createTime integerValue]/1000;
    if (distanceInter < 60) {
        _dateLabel.text = [NSString stringWithFormat:@"发布于%ld秒前", distanceInter];
    }
    else if ((distanceInter > 60) && (distanceInter < 60*60)) {
        _dateLabel.text = [NSString stringWithFormat:@"发布于%ld分钟前", distanceInter/60];
    }
    else if ((distanceInter > 60*60) && (distanceInter < 24*60*60)) {
        _dateLabel.text = [NSString stringWithFormat:@"发布于%ld小时前", distanceInter/60/60];
    }
    else if (distanceInter > 24*60*60 && distanceInter < 7*24*60*60) {
        _dateLabel.text = [NSString stringWithFormat:@"发布于%ld天前", distanceInter/60/60/24];
    }
    else {
        NSString *dateStr = [[NSDate dateWithTimeIntervalSince1970:[productModel.createTime integerValue]/1000] dateStringWithFormat:@"yy.MM.dd"];
        _dateLabel.text = [NSString stringWithFormat:@"发布于%@", dateStr];
    }
//    _avatarImgView.image = [UIImage imageWithColor:kColorHEX(0xff00ff)];
    [_avatarImgView sd_setImageWithURL:[NSURL URLWithString:productModel.avatar] placeholderImage:ImageNamed(@"mine_avatar_icon")];
    _userNameLabel.text = productModel.nickname;//@"芝麻盐儿用户";
    _creditLabel.text = @"信用良好";
}

#pragma mark - Init
- (UIImageView *)contentImgView {
    if (!_contentImgView) {
        _contentImgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.width, KScaleWidth(210.0))];
        [_contentImgView setPartCornerWithRectCorner:UIRectCornerTopRight | UIRectCornerTopLeft cornerRadius:KScaleWidth(13.0)];
    }
    return _contentImgView;
}

- (UILabel *)titleLabel {
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(KScaleWidth(10.0), self.contentImgView.maxY + KScaleWidth(10.0), self.width - KScaleWidth(10.0)*2, KScaleWidth(22.0)) font:KFontSize(16.0) textColor:kColorHEX(0x333333)];
    }
    return _titleLabel;
}

- (UILabel *)priceLabel {
    if (!_priceLabel) {
        _priceLabel = [[UILabel alloc] initWithFrame:CGRectMake(KScaleWidth(10.0), self.titleLabel.maxY, self.width/3 - KScaleWidth(10.0), KScaleWidth(25.0))];
    }
    return _priceLabel;
}

- (UILabel *)dateLabel {
    if (!_dateLabel) {
        _dateLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.width/3, self.titleLabel.maxY + KScaleWidth(7.0), self.width/3*2 - KScaleWidth(10.0), KScaleWidth(14.0)) font:KFontSize(12.0) textColor:kColorHEX(0x999999) textAlignment:NSTextAlignmentRight];
    }
    return _dateLabel;
}

- (UIImageView *)avatarImgView {
    if (!_avatarImgView) {
        _avatarImgView = [[UIImageView alloc] initWithFrame:CGRectMake(KScaleWidth(10.0), self.priceLabel.maxY + KScaleWidth(6.0), KScaleWidth(20.0), KScaleWidth(20.0))];
        [_avatarImgView setCornerRadius:_avatarImgView.height/2];
    }
    return _avatarImgView;
}

- (UILabel *)userNameLabel {
    if (!_userNameLabel) {
        _userNameLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.avatarImgView.maxX + KScaleWidth(3.0), _avatarImgView.y, self.width - _avatarImgView.maxX - KScaleWidth(3.0) - KScaleWidth(54.0), _avatarImgView.height) font:KFontSize(12.0) textColor:kColorHEX(0x999999)];
    }
    return _userNameLabel;
}

- (UILabel *)creditLabel {
    if (!_creditLabel) {
        _creditLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.width - KScaleWidth(10) - KScaleWidth(43.0), self.avatarImgView.y + (_avatarImgView.height - KScaleWidth(14.0))/2, KScaleWidth(43.0), KScaleWidth(14.0)) font:KFontSize(8.0) textColor:kColorHEX(0xF27A3D) textAlignment:NSTextAlignmentCenter];
        _creditLabel.backgroundColor = kColorHEX(0xFFF5EC);
        _creditLabel.text = @"信用良好";
    }
    return _creditLabel;
}

@end
